package com.example.znbk.znbklibrary.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;

import com.example.znbk.znbklibrary.R;
import com.example.znbk.znbklibrary.widget.ScaleButton;

import java.util.List;

/**
 * 自定义底部弹出对话框
 * Created by wangguolong on 2017/9/8.
 */

public class ButtomResultAnswerSheetDialog extends Dialog {

    private Context context;
    private int countOfButton;
    private List<Boolean> answerResult;
    private boolean isCancelable;//控制点击dialog外部是否dismiss
    private boolean isBackCancelable;//控制返回键是否dismiss

    //这里的view其实可以替换直接传layout过来的 因为各种原因没传(lan)
    public ButtomResultAnswerSheetDialog(Context context, int countOfButton, List<Boolean> answerResult, boolean isCancelable, boolean isBackCancelable) {
        super(context, R.style.MyBottomDialog);
        this.context = context;
        this.countOfButton=countOfButton;
        this.answerResult = answerResult;
        this.isCancelable = isCancelable;//这个变量没用，后期去掉
        this.isBackCancelable = isBackCancelable;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        View view = View.inflate(context,R.layout.dialog_bottom_result_answer_sheet,null);
        setContentView(view);//这行一定要写在前面
        setCancelable(isCancelable);//点击外部不可dismiss
        setCanceledOnTouchOutside(isBackCancelable);
        Window window = this.getWindow();
        window.setDimAmount(0f);//去除遮罩
        //设置弹出动画
        window.setWindowAnimations(R.style.BottomDialogAnim);
        window.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(params);

        //展示单个题答题卡信息
        btnPiLiangCreate(countOfButton, R.id.rl_bottomResultAnswerSheet, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("========ff=========","点击了"+v.getId());
            }
        });

    }

    //批量添加圆形按钮的方法
    public void btnPiLiangCreate(int num, int witch, android.view.View.OnClickListener listener) {
        DisplayMetrics dm = new DisplayMetrics();
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        wm.getDefaultDisplay().getMetrics(dm);
        int width = dm.widthPixels;
        int height = dm.heightPixels;
        RelativeLayout layout = findViewById(witch);
        ScaleButton[] SBtn = new ScaleButton[num];
        int j = -1;
        for (int i=0; i<num; i++) {
            SBtn[i]=new ScaleButton(context);
            SBtn[i].setId(i);
            SBtn[i].setTag(i+1);    //为按钮设置一个标记，来确认是按下了哪一个按钮
            SBtn[i].setText(String.valueOf(i+1));
            SBtn[i].setTextSize(12);
            SBtn[i].setGravity(Gravity.CENTER);
            RelativeLayout.LayoutParams btParams = new RelativeLayout.LayoutParams ((width)/8,(width)/8);
            if(answerResult.get(i)) {
                SBtn[i].setBackgroundResource(R.drawable.round_green);
            }else{
                SBtn[i].setBackgroundResource(R.drawable.round_red);
            }

            if (i%5 == 0) {
                j++;
            }
            btParams.setMargins(20,20,20,20);
            btParams.leftMargin = ((width)/7+50)*(i%5); //横坐标定位
            btParams.topMargin = ((width)/7)*j+20; //纵坐标定位
            layout.addView(SBtn[i],btParams); //将按钮放入layout组件
        }
        for (int k = 0; k <= SBtn.length-1; k++) {
            //这里不需要findId，因为创建的时候已经确定哪个按钮对应哪个Id
            SBtn[k].setOnClickListener(listener);
        }
    }

}